package com.ruoyi.puyu.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.RoleEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.puyu.domain.PuyuCoach;
import com.ruoyi.puyu.mapper.PuyuCoachMapper;
import com.ruoyi.puyu.service.IPuyuCoachService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 教练Service业务层处理
 * 
 * @author wusongwen
 * @date 2024-05-01
 */
@Service
public class PuyuCoachServiceImpl implements IPuyuCoachService 
{
    @Autowired
    private PuyuCoachMapper puyuCoachMapper;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysConfigService configService;

    /**
     * 查询教练
     * 
     * @param coachId 教练主键
     * @return 教练
     */
    @Override
    public PuyuCoach selectPuyuCoachByCoachId(Long coachId)
    {
        return puyuCoachMapper.selectPuyuCoachByCoachId(coachId);
    }

    @Override
    public PuyuCoach selectPuyuCoachByUserId(Long userId) {
        return puyuCoachMapper.selectPuyuCoachByUserId(userId);
    }

    /**
     * 查询教练列表
     * 
     * @param puyuCoach 教练
     * @return 教练
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<PuyuCoach> selectPuyuCoachList(PuyuCoach puyuCoach)
    {
        return puyuCoachMapper.selectPuyuCoachList(puyuCoach);
    }

    @Override
    public List<Map<String, Object>> selectCoachUserList() {
        return puyuCoachMapper.selectCoachUserList();
    }

    @Override
    public List<Map<String, Object>> selectCoachList(PuyuCoach puyuCoach) {
        return puyuCoachMapper.selectCoachList(puyuCoach);
    }

    @Override
    public List<Map<String, Object>> selectCoachAllList(PuyuCoach puyuCoach) {
        return puyuCoachMapper.selectCoachAllList(puyuCoach);
    }

    /**
     * 新增教练
     * 
     * @param puyuCoach 教练
     * @return 结果
     */
    @Override
    @Transactional
    public int insertPuyuCoach(PuyuCoach puyuCoach)
    {
        SysUser user = userService.selectUserByPhoneNumber(puyuCoach.getPhone());
        if (null == user) {
            user = new SysUser();
            user.setCreateBy(SecurityUtils.getUsername());
            String password = configService.selectConfigByKey("sys.user.initPassword");
            user.setPassword(SecurityUtils.encryptPassword(password));
            user.setDeptId(puyuCoach.getDeptId());
            user.setUserName(puyuCoach.getPhone());
            user.setNickName(puyuCoach.getCoachName());
            user.setPhonenumber(puyuCoach.getPhone());
            user.setEmail(puyuCoach.getPhone() + "@ptba.cn");
            user.setSex("0");
            user.setStatus("0");
            user.setDelFlag("0");
            Long[] roleIds = new Long[]{RoleEnum.COACH.getRoleId()};
            user.setRoleIds(roleIds);
            userService.insertUser(user);
        }
        puyuCoach.setUserId(user.getUserId());
        puyuCoach.setCreateTime(DateUtils.getNowDate());
        return puyuCoachMapper.insertPuyuCoach(puyuCoach);
    }

    /**
     * 修改教练
     * 
     * @param puyuCoach 教练
     * @return 结果
     */
    @Override
    public int updatePuyuCoach(PuyuCoach puyuCoach)
    {
        puyuCoach.setUpdateTime(DateUtils.getNowDate());
        return puyuCoachMapper.updatePuyuCoach(puyuCoach);
    }

    /**
     * 批量删除教练
     * 
     * @param coachIds 需要删除的教练主键
     * @return 结果
     */
    @Override
    public int deletePuyuCoachByCoachIds(Long[] coachIds)
    {
        return puyuCoachMapper.updatePuyuCoachByCoachIds(coachIds);
    }

    /**
     * 删除教练信息
     * 
     * @param coachId 教练主键
     * @return 结果
     */
    @Override
    public int deletePuyuCoachByCoachId(Long coachId)
    {
        return puyuCoachMapper.deletePuyuCoachByCoachId(coachId);
    }
}
